import moment from 'moment'
export default function ({ app }, inject) {
  const formattedDate = (
    datetime,
    fI18n,
    locale,
    isUTC = false,
    isDay = true
  ) => {
    let values
    let time = datetime * 1000
    let monthArray = [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Aug',
      'Sep',
      'Oct',
      'Nov',
      'Dec',
    ]
    let sDate
    if (isUTC) {
      sDate = moment(time).utc().format('YYYY-MM-DD')
    } else {
      sDate = moment(time).format('YYYY-MM-DD')
    }

    let sDateArry = sDate.split('-')
    let sMonth = monthArray[moment(time).get('month')]
    if (locale === 'zh' || locale === 'tw') {
      values = isDay
        ? `${sDateArry[0]}年${fI18n(sMonth)}${parseInt(sDateArry[2])}日`
        : `${sDateArry[0]}年${fI18n(sMonth)}`
    } else {
      values = isDay
        ? `${fI18n(sMonth)} ${sDateArry[2]} ${sDateArry[0]}`
        : `${fI18n(sMonth)} ${sDateArry[0]}`
    }
    return values
  }

  // 注入全局方法
  app.appFormattedDate = formattedDate
}
